#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>

int main() {
    char hostname[128];
    struct hostent *hostinfo;
    struct in_addr host_address;
    in_addr_t addr;
    int i = 0;

    // 获取主机名
    if (gethostname(hostname, sizeof(hostname)) == -1) {
        perror("gethostname");
        return 1;
    }

    printf("Hostname: %s\n", hostname);

    // 获取主机IP地址信息
    hostinfo = gethostbyname(hostname);
    if (hostinfo == NULL) {
        fprintf(stderr, "Unknown host %s.\n", hostname);
        return 1;
    }

    // 遍历所有的IP地址
    while (hostinfo->h_addr_list[i] != NULL) {
        host_address.s_addr = *(unsigned long *) hostinfo->h_addr_list[i];
        printf("IP Address: %s\n", inet_ntoa(host_address));

        // 将A.B.C.D格式的IP地址转换为32位IP地址
        addr = inet_addr(inet_ntoa(host_address));
        printf("32-bit IP Address: %08x\n", addr);

        i++;
    }

    return 0;
}
